<template>
  <div class="com-container">
    <div class="com-chart" ref="Seller_ref"></div>
  </div>
</template>

<script>
import { getCrossBarData } from '@/API/api.js'
export default {
  name: 'Seller',
  data() {
    return {
      myChart: null,
      allData: null,
      currentPage: 1, // 当前页数
      totalPage: 0, // 总的页数
      timer: null // 定时器标识
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    clearInterval(this.timer)
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化 echarts 实例对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.Seller_ref, 'chalk')
      const initOption = {
        title: {
          text: '▎商家销量统计',
          textStyle: {
            fontSize: 66
          },
          top: 30,
          left: 20
        },
        grid: {
          top: '20%',
          left: '3%',
          bottom: '6%',
          right: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            lineStyle: {
              width: 66,
              color: '#2d3443'
            },
            z: 0
          }
        },
        series: [
          {
            type: 'bar',
            label: {
              show: true,
              position: 'right',
              textStyle: {
                color: '#fff'
              }
            },
            barWidth: 66,
            itemStyle: {
              barBorderRadius: [0, 33, 33, 0],
              color: {
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [
                  {
                    offset: 0,
                    color: 'pink'
                  },
                  {
                    offset: 1,
                    color: 'skyblue'
                  }
                ],
                global: false
              }
            }
          }
        ]
      }
      this.myChart.setOption(initOption)
      // 鼠标经过停止定时器
      this.myChart.on('mouseover', () => {
        clearInterval(this.timer)
      })
      // 鼠标离开开启定时器
      this.myChart.on('mouseout', () => {
        this.startInteval()
      })
    },
    // 向服务器获取数据
    async getData() {
      const { data: res } = await getCrossBarData()
      // console.log(res)
      this.allData = res
      // 从小到大排序
      this.allData.sort((a, b) => {
        return a.value - b.value
      })
      // 每5个元素显示一页
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1
      this.updateChart()
      // 启动定时器
      this.startInteval()
    },
    // 更新图表
    updateChart() {
      // 一页显示5条数据
      const start = (this.currentPage - 1) * 5
      const end = this.currentPage * 5
      const showData = this.allData.slice(start, end)
      // 获取类目名
      const chartName = showData.map((item) => {
        return item.name
      })
      // 获取数值
      const chartValue = showData.map((item) => {
        return item.value
      })
      const option = {
        yAxis: {
          data: chartName
        },
        series: [
          {
            data: chartValue
          }
        ]
      }
      this.myChart.setOption(option)
    },
    // 定时器
    startInteval() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.currentPage++
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1
        }
        this.updateChart()
      }, 3000)
    },
    // 分辨率适配
    screenAdapter() {
      const titleFontSize = (this.$refs.Seller_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      }
      this.myChart.setOption(adapterOption)
      this.myChart.resize()
    }
  }
}
</script>

<style lang="less" scoped></style>
